<?php

namespace app\admin\command;

use fast\Http;
use think\Log;
use think\console\Input;
use think\console\Output;
use think\console\Command;
use think\console\input\Option;
use app\admin\model\wanlshop\Goods;
use addons\wanlshop\library\WanlSdk\Common;
use app\api\service\wanlshop\Goods as GoodsService;

class Sync3 extends Command
{
    protected function configure()
    {
        $this->setName('sync3')
            ->setDescription('sync goods info to baiduyun');
    }

    protected function execute(Input $input, Output $output)
    {
        date_default_timezone_set('PRC');
        ini_set('memory_limit', '-1');
        set_time_limit(0);

        $date = date('Y-m-d H:i:s');
        $output->writeln("开始获取数据，开始时间: $date");
        $appid = '69257164';
        $key = 'CKRKY4xQYlLDb50ES48i4GNw';
        $secret = '0x0AbA0FoRL62bxv6f7L7FS0Sff5lg9h';

        $curl = curl_init();
        $token = $this->getToken();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/product/add?access_token=$token",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_CUSTOMREQUEST => 'POST',
            /*CURLOPT_POSTFIELDS => http_build_query(array(
                'image' => 'iVBORw0KGgoAAAANSUhEUgAAAkoAAAJKCAYAAADTI2qZAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQA...',
                'brief' => []
            )),*/
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/x-www-form-urlencoded',
                'Accept: application/json'
            ),
        ));
        Goods::where('status', 'normal')->chunk(1000, function ($goods) use ($output, $curl) {
            foreach ($goods as $item) {
                $post = http_build_query([
                    'image' => base64_encode(file_get_contents($item['image'])),
                    'brief' => json_encode([
                        'title' => $item['title'],
                        'id' => $item['id']
                    ], 256)
                ]);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
                $response = curl_exec($curl);
                $output->writeln($item['id'] . '--' . json_encode($response, 256));
                Log::log('商品入库：' . json_encode($response, 256));
            }
        });
        curl_close($curl);
    }

    protected function getToken()
    {
        $curl = curl_init();
        $postData = array(
            'grant_type' => 'client_credentials',
            'client_id' => 'CKRKY4xQYlLDb50ES48i4GNw',
            'client_secret' => '0x0AbA0FoRL62bxv6f7L7FS0Sff5lg9h'
        );
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => http_build_query($postData)
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $rtn = json_decode($response);
        return $rtn->access_token;
    }
}